#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

int main(int argc,char* argv[])
{
  int tcp_fd = socket(AF_INET,SOCK_STREAM,0);
  if(tcp_fd < 0)
  {
    perror("socket fail");
    return -1;
  }
  
  struct sockaddr_in server;
  server.sin_port = htons(50000);
  server.sin_family = AF_INET;
  inet_pton(AF_INET,"0.0.0.0",&server.sin_addr);
  int on = 1; // 写一，表示启用

  setsockopt(tcp_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

  int ret = bind(tcp_fd,(struct sockaddr*)&server,sizeof(server));

  listen(tcp_fd,1);
  struct sockaddr_in client;
  socklen_t len = sizeof(client);
  int con_fd = accept(tcp_fd,(struct sockaddr*)&client,&len);

  char buf[1024] = {0};
  char name[10];
  read(con_fd,name,10);
  printf("file name is %s\n",name);
  sprintf(buf,"aaa/%s",name);
  int file_fd = open(buf,O_RDWR | O_CREAT,0666);
  if(file_fd < 0)
  {
    printf("create fail");
    return -1;
  }

  usleep(2000);

  while(1)
  {
    memset(buf,0,1024);
    ret = read(con_fd,buf,1024);
    if(ret == 1024)
    {
      write(file_fd,buf,1024);
    }
    else if(ret < 1024)
    {
      write(file_fd,buf,strlen(buf));
      break;
    }
  }

  close(file_fd);
  close(con_fd);
  close(tcp_fd);
  return 0;
}
 
  struct sockaddr_in server;
  server.sin_port = htons(50000);
  server.sin_family = AF_INET;
  inet_pton(AF_INET,"0.0.0.0",&server.sin_addr);
  int on = 1; // 写一，表示启用

  setsockopt(tcp_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

  int ret = bind(tcp_fd,(struct sockaddr*)&server,sizeof(server));

  listen(tcp_fd,1);
  struct sockaddr_in client;
  socklen_t len = sizeof(client);
  int con_fd = accept(tcp_fd,(struct sockaddr*)&client,&len);

  char buf[1024] = {0};
  char name[10];
  read(con_fd,name,10);
  printf("file name is %s\n",name);
  sprintf(buf,"aaa/%s",name);
  int file_fd = open(buf,O_RDWR | O_CREAT,0666);
  if(file_fd < 0)
  {
    printf("create fail");
    return -1